package com.siblings.loan.ui.loan.fm

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.lib.base.base.BaseFragment
import com.siblings.loan.R
import com.siblings.loan.databinding.FmLoanDataBinding
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.ui.tab.act.SibMainAct
import com.siblings.loan.ui.user.act.SibMessageAct
import com.siblings.loan.utils.event.SibEventType
import com.siblings.loan.utils.helper.SharedHelper
import org.json.JSONObject

/**
 * SiblingsLoan
 * @Author wwdeng
 * @CreateTime: 2024/7/8 18:12
 * @Annotation：数据安全
 */
class SibLoanSafetyFm: BaseFragment() {
    lateinit var binding: FmLoanDataBinding
    override fun getSibRootView(inflater: LayoutInflater): View {
        binding = FmLoanDataBinding.inflate(inflater)
        return binding.root
    }

    override fun initSibView() {
        setSibTitleBarColor(Color.parseColor("#FFFFFF"))
        setSibTextTitle(getString(R.string.privacy_policy_title))
        setSibBackBtnRes(R.mipmap.pic_title_menu_theme_icon)
        setSibRightBtnRes(R.mipmap.pic_title_message_theme_icon) //拦截返回
        isInterceptBack(true)

        initWebViewSettings()
    }

    override fun initSibListener() {
        //菜单
        rootViewBinding.ivBaseBack.setOnClickListener {
            val tabAct = context as SibMainAct
            tabAct.showSibMenuFragment("Data Safety Page")
        }

        //消息
        rootViewBinding.ivFun.setOnClickListener {
            onIntent(SibMessageAct::class.java)
        }
    }

    override fun initSibData() {
        val commonData = JSONObject(SharedHelper.getSibSPString(SharedHelper.SIB_SPKEY.SIBLINGS_COMMON))
        val customize = commonData.optJSONObject("customize")
        if (customize != null) {
            val privacyPolicyLink = customize.optString("privacyPolicyLink")
            binding.webView.loadUrl(privacyPolicyLink)
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebViewSettings() {
        binding.webView.setInitialScale(100)
        binding.webView.webChromeClient = mWebChromeClient
        binding.webView.webViewClient = mWebClient
        val webSettings = binding.webView.settings
        webSettings.setSupportZoom(true)
        try {
            webSettings.javaScriptEnabled = true
            webSettings.textZoom = 100
            webSettings.domStorageEnabled = true
            webSettings.builtInZoomControls = true
            webSettings.displayZoomControls = true
            webSettings.useWideViewPort = true
            webSettings.loadWithOverviewMode = true
            webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
            webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            webSettings.allowUniversalAccessFromFileURLs = true
            webSettings.allowFileAccessFromFileURLs = true
            webSettings.allowContentAccess = true
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val mWebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)

            binding.progressBar.progress = newProgress
            if (newProgress == 100) {
                binding.progressBar.visibility = View.GONE
            } else {
                binding.progressBar.visibility = View.VISIBLE
            }
        }
    }

    private val mWebClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?,
        ): Boolean {
            return shouldOverrideUrlLoading(view, request!!.url.toString())
        }

        @Deprecated("Deprecated in Java", ReplaceWith("false"))
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return false
        }
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_PRIVACY_POLICY_START,"")
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_PRIVACY_POLICY_END,"")
    }

}